﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DataLib.DBUtility;

namespace DataLib.DAL
{
	/// <summary>
	/// 数据访问类:LoadNum
	/// </summary>
	public partial class LoadNum
    {


        public LoadNum()
		{}
		#region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "LoadNum");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int? id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from LoadNum");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DataLib.Model.LoadNum model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into LoadNum(");
            strSql.Append("loadnum,addressid,Rubbisharea,belongarea,customerid,collectdate,goodsInfo,totalWeight,finishWeight,status,saleman,RubbishType,Memo,cumcode,cumname,projectid,projectcode,projectname,status_tally,finishtallyweight,cjoname,printname,labname)");
            strSql.Append(" values (");
            strSql.Append("@loadnum,@addressid,@Rubbisharea,@belongarea,@customerid,@collectdate,@goodsInfo,@totalWeight,@finishWeight,@status,@saleman,@RubbishType,@Memo,@cumcode,@cumname,@projectid,@projectcode,@projectname,@status_tally,@finishtallyweight,@cjoname,@printname,@labname)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@loadnum", SqlDbType.NVarChar,30),
					new SqlParameter("@addressid", SqlDbType.Int,4),
					new SqlParameter("@Rubbisharea", SqlDbType.NVarChar,50),
					new SqlParameter("@belongarea", SqlDbType.NVarChar,30),
					new SqlParameter("@customerid", SqlDbType.Int,4),
					new SqlParameter("@collectdate", SqlDbType.DateTime),
					new SqlParameter("@goodsInfo", SqlDbType.NVarChar,200),
					new SqlParameter("@totalWeight", SqlDbType.Decimal,9),
					new SqlParameter("@finishWeight", SqlDbType.Decimal,9),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@saleman", SqlDbType.NVarChar,30),
					new SqlParameter("@RubbishType", SqlDbType.NVarChar,200),
                    new SqlParameter("@Memo", SqlDbType.NVarChar,200),
                    new SqlParameter("@cumcode", SqlDbType.VarChar,100),
                    new SqlParameter("@cumname", SqlDbType.VarChar,200),
                    new SqlParameter("@projectid", SqlDbType.Int,4),
					new SqlParameter("@projectcode", SqlDbType.VarChar,100),
					new SqlParameter("@projectname", SqlDbType.VarChar,200),
					new SqlParameter("@status_tally", SqlDbType.Int,4),
					new SqlParameter("@finishtallyweight", SqlDbType.Decimal,10),
                    new SqlParameter("@cjoname",SqlDbType.VarChar,200),
                    new SqlParameter("@printname",SqlDbType.VarChar,200),
                    new SqlParameter("@labname",SqlDbType.VarChar,200)
                                        };
            parameters[0].Value = model.loadnum;
            parameters[1].Value = model.addressid;
            parameters[2].Value = model.Rubbisharea;
            parameters[3].Value = model.belongarea;
            parameters[4].Value = model.customerid;
            parameters[5].Value = model.collectdate;
            parameters[6].Value = model.goodsInfo;
            parameters[7].Value = model.totalWeight;
            parameters[8].Value = model.finishWeight;
            parameters[9].Value = model.status;
            parameters[10].Value = model.saleman;
            parameters[11].Value = model.RubbishType;
            parameters[12].Value = model.Memo;
            parameters[13].Value = model.cumcode;
            parameters[14].Value = model.cumname;
            parameters[15].Value = model.projectid;
            parameters[16].Value = model.projectcode;
            parameters[17].Value = model.projectname;
            parameters[18].Value = model.status_tally;
            parameters[19].Value = model.finishtallyweight;
            parameters[20].Value = model.cjoname;
            parameters[21].Value = model.printname;
            parameters[22].Value = model.labname;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

         /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DataLib.Model.LoadNum model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update LoadNum set ");
            strSql.Append("loadnum=@loadnum,");
            strSql.Append("addressid=@addressid,");
            strSql.Append("Rubbisharea=@Rubbisharea,");
            strSql.Append("belongarea=@belongarea,");
            strSql.Append("customerid=@customerid,");
            strSql.Append("collectdate=@collectdate,");
            strSql.Append("goodsInfo=@goodsInfo,");
            strSql.Append("totalWeight=@totalWeight,");
            strSql.Append("finishWeight=@finishWeight,");
            strSql.Append("status=@status,");
            strSql.Append("saleman=@saleman,");
            strSql.Append("RubbishType=@RubbishType,");
            strSql.Append("Memo=@Memo");
         //   strSql.Append("createdate=@createdate");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@loadnum", SqlDbType.NVarChar,30),
					new SqlParameter("@addressid", SqlDbType.Int,4),
					new SqlParameter("@Rubbisharea", SqlDbType.NVarChar,50),
					new SqlParameter("@belongarea", SqlDbType.NVarChar,30),
					new SqlParameter("@customerid", SqlDbType.Int,4),
					new SqlParameter("@collectdate", SqlDbType.DateTime),
					new SqlParameter("@goodsInfo", SqlDbType.NVarChar,200),
					new SqlParameter("@totalWeight", SqlDbType.Decimal,9),
					new SqlParameter("@finishWeight", SqlDbType.Decimal,9),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@saleman", SqlDbType.NVarChar,30),
					new SqlParameter("@RubbishType", SqlDbType.NVarChar,200),
                    new SqlParameter("@Memo", SqlDbType.NVarChar,200),
                //    new SqlParameter("@createdate", SqlDbType.DateTime),
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.loadnum;
            parameters[1].Value = model.addressid;
            parameters[2].Value = model.Rubbisharea;
            parameters[3].Value = model.belongarea;
            parameters[4].Value = model.customerid;
            parameters[5].Value = model.collectdate;
            parameters[6].Value = model.goodsInfo;
            parameters[7].Value = model.totalWeight;
            parameters[8].Value = model.finishWeight;
            parameters[9].Value = model.status;
            parameters[10].Value = model.saleman;
            parameters[11].Value = model.RubbishType;
            parameters[12].Value = model.Memo;
        //    parameters[13].Value = model.createdate;
            parameters[13].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from LoadNum ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from LoadNum ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.LoadNum GetModel(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,loadnum,addressid,Rubbisharea,belongarea,customerid,collectdate,goodsInfo,totalWeight,finishWeight,status,saleman,RubbishType,Memo,cumcode,cumname,projectid,projectcode,projectname,status_tally,finishtallyweight,cjoname,printname,labname from LoadNum ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            DataLib.Model.LoadNum model = new DataLib.Model.LoadNum();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.LoadNum DataRowToModel(DataRow row)
        {
            DataLib.Model.LoadNum model = new DataLib.Model.LoadNum();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["loadnum"] != null)
                {
                    model.loadnum = row["loadnum"].ToString();
                }
                if (row["addressid"] != null && row["addressid"].ToString() != "")
                {
                    model.addressid = int.Parse(row["addressid"].ToString());
                }
                if (row["Rubbisharea"] != null)
                {
                    model.Rubbisharea = row["Rubbisharea"].ToString();
                }
                if (row["belongarea"] != null)
                {
                    model.belongarea = row["belongarea"].ToString();
                }
                if (row["customerid"] != null && row["customerid"].ToString() != "")
                {
                    model.customerid = int.Parse(row["customerid"].ToString());
                }
                if (row["collectdate"] != null && row["collectdate"].ToString() != "")
                {
                    model.collectdate = DateTime.Parse(row["collectdate"].ToString());
                }
                if (row["goodsInfo"] != null)
                {
                    model.goodsInfo = row["goodsInfo"].ToString();
                }
                if (row["totalWeight"] != null && row["totalWeight"].ToString() != "")
                {
                    model.totalWeight = decimal.Parse(row["totalWeight"].ToString());
                }
                if (row["finishWeight"] != null && row["finishWeight"].ToString() != "")
                {
                    model.finishWeight = decimal.Parse(row["finishWeight"].ToString());
                }
                if (row["status"] != null && row["status"].ToString() != "")
                {
                    model.status = int.Parse(row["status"].ToString());
                }
                if (row["saleman"] != null)
                {
                    model.saleman = row["saleman"].ToString();
                }
                if (row["RubbishType"] != null)
                {
                    model.RubbishType = row["RubbishType"].ToString();
                }
                if (row["Memo"] != null)
                {
                    model.Memo = row["Memo"].ToString();
                }
                if (row["cumcode"] != null)
                {
                    model.cumcode = row["cumcode"].ToString();
                }
                if (row["cumname"] != null)
                {
                    model.cumname = row["cumname"].ToString();
                }
                if (row["projectid"] != null && row["projectid"].ToString() != "")
                {
                    model.projectid = int.Parse(row["projectid"].ToString());
                }
                if (row["projectcode"] != null)
                {
                    model.projectcode = row["projectcode"].ToString();
                }
                if (row["projectname"] != null)
                {
                    model.projectname = row["projectname"].ToString();
                }
                if (row["status_tally"] != null && row["status_tally"].ToString() != "")
                {
                    model.status_tally = int.Parse(row["status_tally"].ToString());
                }
                if (row["finishtallyweight"] != null && row["finishtallyweight"].ToString() != "")
                {
                    model.finishtallyweight = decimal.Parse(row["finishtallyweight"].ToString());
                }
                if (row["cjoname"] != null)
                {
                    model.cjoname = row["cjoname"].ToString().Trim();
                }
                if (row["printname"] != null)
                {
                    model.printname = row["printname"].ToString().Trim();
                }
                if (row["labname"] != null)
                {
                    model.labname = row["labname"].ToString().Trim();
                }

            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,loadnum,addressid,Rubbisharea,belongarea,customerid,collectdate,goodsInfo,totalWeight,finishWeight,status,saleman,RubbishType,Memo,cumcode,cumname,projectid,projectcode,projectname,status_tally,finishtallyweight,cjoname,printname,labname ");
            strSql.Append(" FROM LoadNum ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,loadnum,addressid,Rubbisharea,belongarea,customerid,collectdate,goodsInfo,totalWeight,finishWeight,status,saleman,RubbishType,Memo,cumcode,cumname,projectid,projectcode,projectname,status_tally,finishtallyweight,cjoname,printname,labname ");
            strSql.Append(" FROM LoadNum ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM LoadNum ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from LoadNum T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 获取所有废弃物的Load数据集
        /// </summary>
        /// <returns></returns>
        public DataTable GetLoadKH()
        {
            DataTable dt = new DataTable();//from 后面应该直接跟表名，而不该带上数据库名导致无法访问 by 2013.04.03  梁玲
            string strsql = @"SELECT l.id
      ,l.LoadNum
      ,[addressid]
      ,[belongarea]
      ,[customerid]
      ,[collectdate]
      ,[totalWeight]
      ,[finishWeight]
      ,l.saleman
      ,[RubbishType]
      ,[Memo]
      ,l.cumcode customercode
      ,l.cumname companyname
      ,RubbishData.LDH
      ,RubbishData.Remark
      ,RubbishData.rubbiscode
      ,RubbishData.rubbisname
      ,RubbishData.rubbWeight
      ,RubbishData.id rid
      ,cjoname,printname,labname
      ,isnull(RubbishData.id,0)
			modelstatus
  FROM [LoadNum] l 
	left join RubbishData on l.id=RubbishData.loadlnumid
  
  where RubbishType is not null 
  order by l.id desc";
            dt = DbHelperSQL.Query(strsql).Tables[0];
            return dt;
        }


        public DataTable GetLoadKHBywhere(string where)
        {
            DataTable dt = new DataTable();
            string strsql = @"SELECT TOP 1000 LoadNum.[id]
      ,Loadnum.[LoadNum]
      ,[addressid]
      ,[belongarea]
      ,[customerid]
      ,[collectdate]
      ,[totalWeight]
      ,[finishWeight]
      ,LoadNum.[saleman]
      ,[RubbishType]
       ,[Memo]
    ,Cumtomer.customercode
      ,Cumtomer.companyname
      ,RubbishData.LDH
      ,RubbishData.Remark
      ,RubbishData.rubbiscode
      ,RubbishData.rubbisname
      ,RubbishData.rubbWeight
  FROM [LoadNum] left join RubbishData on [LoadNum].id=RubbishData.loadlnumid,Cumtomer
  where RubbishType is not null 
	and LoadNum.customerid=Cumtomer.id 
  " + where;
            dt = DbHelperSQL.Query(strsql).Tables[0];
            return dt;
        }

        /// <summary>
        /// 根据Load号获取Model
        /// </summary>
        /// <param name="loadno">LOAD号</param>
        /// <returns></returns>
        public Model.LoadNum GetLoadID(string loadno)
        {
            Model.LoadNum model = new Model.LoadNum();
            if (loadno != null && loadno.Trim() != "")
            {
                DataTable dt = GetList("loadnum='" + loadno + "'").Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    model = DataRowToModel(dt.Rows[0]);
                }
                else
                {
                    model = null;
                }

            }
            return model;
        }

        /// <summary>
        /// 取得Load节点
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public DataTable GetTreeVieww(string no,string notype)
        {
            DataTable dt = new DataTable();

            SqlParameter[] sqlparameter ={
                new SqlParameter("@no",SqlDbType.VarChar,40),
                new SqlParameter("@notype",SqlDbType.VarChar,20)
             };
            sqlparameter[0].Value = no;
            sqlparameter[1].Value = notype;
         
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_Data_Maintenance", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;

        }

        /// <summary>
        /// 显示Load单下面所有CJO的显示人员
        /// </summary>
        /// <param name="loadid"></param>
        /// <returns></returns>
        public string SalemanByLoad(int loadid)
        {
            string saleman = "";
            DataTable dt = new Cjo().GetList("loadnumid=" + loadid.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                saleman = dt.Rows[0]["sealman_show"].ToString();
                //for (int i = 1; i < dt.Rows.Count; i++)
                //{
                //    saleman += "," + dt.Rows[0]["sealman_show"].ToString();
                //}
            }
            return saleman;
        }

        /// <summary>
        /// 根据Loadid取项目ID
        /// </summary>
        /// <param name="loadid"></param>
        /// <returns></returns>
        public int GetProidByLoadid(string loadid)
        {
            int proid = 0;
            DataTable dt = new Cjo().GetList("loadnumid=" + loadid.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                string prid = "";
                prid = dt.Rows[0]["addid"].ToString();
                if (prid != null && prid != "")
                {
                    Model.ProjectsRelation prmodel = new ProjectRelation().GetModel(int.Parse(prid));
                    proid = (int)prmodel.ProjectID;
                }
                //for (int i = 1; i < dt.Rows.Count; i++)
                //{
                //    saleman += "," + dt.Rows[0]["sealman_show"].ToString();
                //}
            }
            return proid;
        }
		#endregion  ExtensionMethod
	}
}

